package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.DistributedLock;
import com.sc.nft.dao.ExternalLinkIconDao;
import com.sc.nft.entity.ExternalLinkIcon;
import com.sc.nft.entity.dto.ExternalLinkIconIconUpdateDTO;
import com.sc.nft.entity.dto.ExternalLinkIconPageAdminDTO;
import com.sc.nft.entity.dto.ExternalLinkIconSaveAdminDTO;
import com.sc.nft.entity.vo.ExternalLinkIconPageAdminVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ExternalLinkIconService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 外链图标管理表(ExternalLinkIcon)表服务实现类
 *
 * @author makejava
 * @since 2023-03-06 13:41:35
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ExternalLinkIconServiceImpl implements ExternalLinkIconService {

    private final ExternalLinkIconDao externalLinkIconDao;

    @Override
    @DistributedLock(value = {"ExternalLinkIconService", "deleteById", "#{id}"}, message = "禁止频繁操作")
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return externalLinkIconDao.deleteById(id);
    }

    @Override
    public Page<ExternalLinkIconPageAdminVO> pageAdmin(ExternalLinkIconPageAdminDTO externalLinkIconPageAdminDTO) {
        return externalLinkIconDao.pageAdmin(externalLinkIconPageAdminDTO);
    }

    @Override
    @DistributedLock(value = {"ExternalLinkIconService", "updateById", "#{externalLinkIconIconUpdateDTO.id}"}, message = "禁止频繁操作")
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(ExternalLinkIconIconUpdateDTO externalLinkIconIconUpdateDTO) {
        Boolean sameName = externalLinkIconDao.sameName(externalLinkIconIconUpdateDTO.getId(), externalLinkIconIconUpdateDTO.getIconName());
        if (sameName) {
            throw new GlobalRunTimeException("存在同名数据,禁止修改");
        }
        ExternalLinkIconIconUpdateDTO info = new ExternalLinkIconIconUpdateDTO();
        BeanUtil.copyProperties(externalLinkIconIconUpdateDTO, info);
        externalLinkIconIconUpdateDTO.setId(null);
        if (BeanUtil.isEmpty(externalLinkIconIconUpdateDTO)) {
            throw new GlobalRunTimeException("修改对象不能为空");
        }
        return externalLinkIconDao.updateById(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DistributedLock(value = {"ExternalLinkIconService", "save"}, message = "禁止频繁操作")
    public Boolean save(ExternalLinkIconSaveAdminDTO externalLinkSaveAdminDTO) {
        // 判断当前是否有同名的条目
        Boolean sameName = externalLinkIconDao.sameName(externalLinkSaveAdminDTO.getIconName());
        if (sameName) {
            throw new GlobalRunTimeException("存在同名数据,禁止修改");
        }
        ExternalLinkIcon externalLinkIcon = new ExternalLinkIcon();
        BeanUtil.copyProperties(externalLinkSaveAdminDTO, externalLinkIcon);
        return externalLinkIconDao.save(externalLinkIcon);
    }

    @Override
    public Boolean selectByIdExist(Long id) {
        return externalLinkIconDao.selectByIdExist(id);
    }


}
